package com.skywo.demo1.common.security

import com.alibaba.fastjson2.JSON
import com.skywo.demo1.common.result.RF
import com.skywo.demo1.common.result.ResultCode
import org.springframework.security.core.AuthenticationException
import org.springframework.security.web.authentication.AuthenticationFailureHandler
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class MyAuthenticationFailureHandler : AuthenticationFailureHandler {
    override fun onAuthenticationFailure(
        request: HttpServletRequest,
        response: HttpServletResponse,
        exception: AuthenticationException
    ) {
        val errMessage = exception.message

        //返回响应
        response.contentType = "application/json;charset=UTF-8"
        response.writer.println(JSON.toJSONString(RF.fail(ResultCode.FORBIDDEN, errMessage)))
    }
}